package com.astruyk.battlegroundAndroid;

import com.astruyk.battlegroundAndroid.data.CapturePoint;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TownListRowAdapter extends BaseAdapter {
	
	public TownListRowAdapter(Context context){
		_inflater = LayoutInflater.from(context);
		
		String[] projection = new String[] {
				CapturePoint.NAME,
				CapturePoint.ORIG_COUNTRY
		};
		
		_cursor = context.getContentResolver().query(CapturePoint.CONTENT_URI, projection, null, null, null);
		
		_nameColumn = _cursor.getColumnIndex(CapturePoint.NAME);
		_origCountryColumn = _cursor.getColumnIndex(CapturePoint.ORIG_COUNTRY);
	}
	
	private Cursor _cursor;
	private LayoutInflater _inflater;
	
	private final int _nameColumn;
	private final int _origCountryColumn;

	public int getCount() {
		return _cursor.getCount();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView;
		if(convertView == null){
			rowView = _inflater.inflate(R.layout.town_list_item, null);
		}else{
			rowView = convertView;
		}
		
		//Move to the indicated position
		_cursor.moveToPosition(position);
		
		//Set the text for the row
		TextView townNameView = (TextView)rowView.findViewById(R.id.town_name);
		townNameView.setText(_cursor.getString(_nameColumn));

		//Set the correct icon for this row
		ImageView iconView = (ImageView)rowView.findViewById(R.id.town_icon);
		switch(_cursor.getInt(_origCountryColumn)){
		case Constants.COUNTRY_BRITAIN:
			iconView.setImageResource(R.drawable.roundel_britain);
			break;
		case Constants.COUNTRY_FRANCE:
			iconView.setImageResource(R.drawable.roundel_france);
			break;
		case Constants.COUNTRY_GERMANY:
			iconView.setImageResource(R.drawable.roundel_germany);
			break;
		default:
			iconView.setImageResource(R.drawable.roundel_unknown);
			break;
		}
		return rowView;
	}

}
